var formatMapList = {0 : "BMP", 1 : "JPEG", 2 : "GIF", 3 : "PSD", 4 : "PNG", 5 : "TIFF", 6 : "TGA", 7 : "EPS", 100 : "AVI", 101 : "MOV", 102 : "RMVB", 103 : "RM", 104 :"FLV", 105 : "MP4", 106 : "3GP", "200" : "HTML"};
// function setDiv() {
//     document.getElementById("_select_loc").style.display = "none";
// }
//
// function showSelectPackage() {
//     document.getElementById("_select_loc").style.display = "none";
// }
//
// function showSelectLoc() {
//     document.getElementById("_select_loc").style.display = "inline";
// }
function getIndustries() {
    var tbody=window.document.getElementById("_industry");
    $.ajax({
        url:"../../industry/all",
        type:"GET",
        dataType:"json",
        success:function(data){
            console.log(data);
            if(data.code==0){
                var str="<option value=''>选择行业</option>";
                var tdata=data.object;
                for( i in tdata) {
                    str += "<option value='" + tdata[i].id + "'>" + tdata[i].name + "</option>";
                }
                tbody.innerHTML =str;
            }
        },
        error:function(){
            alert("search fails！");
        }
    });
}

function industryLinkage() {
    $("#_industry").on("change",function(){
        getQualificationsByIndustry();
    });
}

function getQualificationsByIndustry() {
    var tbody=window.document.getElementById("_qualification_data");
    // 获取选中的行业
    var optionIndustry=$("#_industry option:selected");
    var qualificationValid = 0;
    $.ajax({
        url:"../../qualification/industry",
        type:"POST",
        data:{industryId:optionIndustry.val()},
        async: false,
        contentType:"application/x-www-form-urlencoded; charset=utf-8",
        dataType:"json",
        success:function(data){
            console.log(data);
            if(data.code==0){
                var str = "<span>行业所需资质：</span>";
                var tdata=data.object;
                for( i in tdata){
                    var j = parseInt(i)+1;
                    str +="<table><tr>"+
                        "<td height='20' bgcolor='#FFFFFF'><div align='center'><span class='STYLE1'>"+ j +"</td>"+
                        "<td height='20' bgcolor='#FFFFFF'><div align='center'><span class='STYLE1'>"+tdata[i].name+"</td>"+
                        "<td height='20' bgcolor='#FFFFFF'><div align='center'><span class='STYLE1'>"+convertQualiStateToString(tdata[i].state)+"</td>"+
                        "</tr></table>";
                    if (tdata[i].state != 1) {
                        qualificationValid = -1;
                    }
                }
                tbody.innerHTML =str;
            }
        },
        error:function(){
            alert("search fails！");
        }
    });

    if (qualificationValid == -1) {
        document.getElementById("_plan_apply").style.display = "none";

        alert("所需资质不符合条件，请上传或等待审核!");
    } else {
        document.getElementById("_plan_apply").style.display = "inline";
    }
}

function convertQualiStateToString(state) {
    var str = "";
    if (state == -1) {
        str += " (未上传)";
    }
    if (state == 0) {
        str += " (未审核)";
    }
    if (state == 1) {
        str += " (审核通过)";
    }
    if (state == 2) {
        str += "(审核失败，查看详情)";
    }
    return str;
}

//获得站点信息
function getPackageInfo(){
    var tbody=window.document.getElementById("resource_data");
    $.ajax({
        url:"../../package/seller",
        type:"GET",
        dataType:"json",
        success:function(data){
            console.log(data);
            if(data.code==0){
                var str="";
                var tdata=data.object;
                for( i in tdata){
                    str +=
                        "<span class=\"STYLE1\">" +
                        "<input type='checkbox' name='packages' value='" + tdata[i].id+ "'>id:" + tdata[i].id + " " + tdata[i].name+"" +
                        "（内含 " + tdata[i].locsNum + " 个资源位）" +
                        "</span><br>";
                }
                tbody.innerHTML =str;
            }
        },
        error:function(){
            alert("search fails！");
        }
    });
}

function addPackages() {
    var locsArray = [];
    var locsString = document.getElementById("_packageIds").value;
    if (locsString != null ) {
        locsArray = locsString.split(",");
    }
    var newLocs = document.getElementsByName("packages");
    for (k in newLocs) {
        if (newLocs[k].checked) {
            if (locsArray.indexOf(newLocs[k].value.toString()) <= -1) {
                locsArray.push(newLocs[k].value.toString());
            }
        }
    }
    var str = locsArray.toString();
    if (str.charAt(0) == ',') {
        str = str.substring(1, str.length);
    }
    document.getElementById("_packageIds").value = str;
    getAvailableLocsNum(str);
    getTemplate();
}

function deletePackages() {
    var locsArray = [];
    var locsString = document.getElementById("_packageIds").value;
    if (locsString != null ) {
        locsArray = locsString.split(",");
    }
    var newLocs = document.getElementsByName("packages");
    for (k in newLocs) {
        if (newLocs[k].checked) {
            var locsArrayIndex = locsArray.indexOf(newLocs[k].value.toString());
            // 原数组中存在需要删除的元素
            if (locsArrayIndex > -1) {
                locsArray.splice(locsArrayIndex,1);
            }
        }
    }
    var str = locsArray.toString();
    if (str.charAt(0) == ',') {
        str = str.substring(1, str.length);
    }
    document.getElementById("_packageIds").value = str;
}


function getAvailableLocsNum(packageIds) {
    var optionType=$("#_type option:selected");  //获取选中的项
    $.ajax({
        url:"../../package/locs",
        type:"POST",
        data:{packageIds:packageIds, type:optionType.val()},
        async: false,
        contentType:"application/x-www-form-urlencoded; charset=utf-8",
        dataType:"json",
        success:function(data){
            console.log(data);
            if(data.code==0){
                var tdata=data.object;
                document.getElementById("_locsNum").innerText = tdata.num;
                document.getElementById("_locs").innerText = tdata.locs;
            }
        },
        error:function(){
            alert("search fails！");
        }
    });

}

function getDateAndTime() {
    var time = new Date();
    var day = ("0" + time.getDate()).slice(-2);
    var month = ("0" + (time.getMonth() + 1)).slice(-2);
    var today = time.getFullYear() + "-" + (month) + "-" + (day);
    $('#_start_date').val(today);
    $('#_end_date').val(today);
}


function getTemplate() {
    var tbody=window.document.getElementById("material_data");
    var locIds = document.getElementById("_locs").innerText;
    var type = document.getElementById("_type").value;
    $.ajax({
        url:"../../template/locs",
        type:"POST",
        data:{locIds:locIds, type:type},
        async: false,
        contentType:"application/x-www-form-urlencoded; charset=utf-8",
        dataType:"json",
        success:function(data){
            console.log(data);
            if(data.code==0){
                var str="";
                var tdata=data.object;
                for( i in tdata){
                    var j = parseInt(i)+1;
                    str +="<input type='hidden' id='_template_id"+ tdata[i].id +"' name='templateIds' value='" + tdata[i].id + "'> " +
                        "<tr>"+
                        "<td height='20' bgcolor='#FFFFFF'><div align='center'><span class='STYLE1'>" + j + "</span></td>"+
                        "<td height='20' bgcolor='#FFFFFF'><div align='center'><span class='STYLE1'>"+convertFormatToString(tdata[i].format)+"</td>"+
                        "<td height='20' bgcolor='#FFFFFF'><div align='center'><span class='STYLE1'>"+tdata[i].maxSize+"K</td>"+
                        "<td height='20' bgcolor='#FFFFFF'><div align='center'><span class='STYLE1'>"+tdata[i].height+"像素</td>" +
                        "<td height='20' bgcolor='#FFFFFF'><div align='center'><span class='STYLE1'>"+tdata[i].width+"像素</td>" +
                        "<td height='20' bgcolor='#FFFFFF'><div align='center'><span class='STYLE1'>" +
                            "<input type='text' id='_material_url"+ tdata[i].id +"' name='materialUrls'></span></td>"+
                        "<td height='20' bgcolor='#FFFFFF'><div align='center'><span class='STYLE1'>" +
                            "<input type='text' id='_seconds"+ tdata[i].id +"' name='materialSeconds'></span></td>"+
                        "</tr>";
                    tbody.innerHTML =str;
                }
            }
        },
        error:function(){
            alert("search fails！");
        }
    });

}

function convertFormatToString(list) {
    var str = "";
    for ( var i = 0; i < list.length; i++){
        str += formatMapList[list[i]];
        str += "; "
    }
    return str;
}

function addSubmitButtonClick() {
    var fdata = $("#plan_add_form").serialize();
    $.param(fdata);
    $.ajax({
        url:"../../plan/new",
        type:"POST",
        data:fdata,
        async: false,
        contentType:"application/x-www-form-urlencoded; charset=utf-8",
        dataType:"json",
        success:function(result){
            if(result.code==0){
                alert(result.message);
                location.href="./plan_list.html";
            }else if(result.code==1){
                alert(result.message);
            }
        }
    });

}




function getPlanInfo() {
    var tbody=window.document.getElementById("plan_data");
    var fdata = $("#plans_form").serialize();
    $.param(fdata);
    $.ajax({
        url:"../../plan/seller",
        type:"POST",
        data:fdata,
        async: false,
        contentType:"application/x-www-form-urlencoded; charset=utf-8",
        dataType:"json",
        success:function(data){
            console.log(data);
            if(data.code==0){
                var str="";
                var tdata=data.object;
                for( i in tdata){
                    var j = parseInt(i)+1;
                    str +="<tr>"+
                        "<td height='20' bgcolor='#FFFFFF'><div align='center'><span class='STYLE1'>"+ j +"</td>"+
                        "<td height='20' bgcolor='#FFFFFF'><div align='center'><span class='STYLE1'>"+ tdata[i].name +"</td>"+
                        "<td height='20' bgcolor='#FFFFFF'><div align='center'><span class='STYLE1'>"+ tdata[i].industryName +"</td>"+
                        "<td height='20' bgcolor='#FFFFFF'><div align='center'><span class='STYLE1'>"+ convertTypeToString(tdata[i].type) +"</td>" +
                        "<td height='20' bgcolor='#FFFFFF'><div align='center'><span class='STYLE1'>"+ tdata[i].startDate.substr(0, 10) + " 至 " + tdata[i].endDate.substr(0, 10) +"</td>" +
                        "<td height='20' bgcolor='#FFFFFF'><div align='center'><span class='STYLE1'>"+ tdata[i].locsNum +"</td>" +
                        "<td height='20' bgcolor='#FFFFFF'><div align='center'><span class='STYLE1'>"+ convertPlanStateToString(tdata[i].state, tdata[i].id) +"</td>" +
                        "<td height='20' bgcolor='#FFFFFF'><div align='center'><span class='STYLE1'>" +
                        "<input type='button' value='编辑' id='edit_button'+" + tdata[i].id + " onclick='editButtonClick(" + tdata[i] + ")'" +"</input>"+
                        "<input type='button' value='删除' id='del_button'+" + tdata[i].id + " onclick='deleteButtonClick( " + tdata[i].id + ")'" +"</input></tr>";
                        "</tr>";
                    tbody.innerHTML =str;
                }
            }
        },
        error:function(){
            alert("search fails！");
        }
    });

}

function convertTypeToString(type) {
    if (type == 0) {
        return "图片";
    }
    if (type == 1) {
        return "视频";
    }
    if (type == 2) {
        return "html";
    }
}


function convertPlanStateToString(state, id) {
    if (state == 0) {
        return "未审核";
    }
    if (state == 1) {
        return "审核成功";
    }
    if (state == 2) {
        return "<label onclick='previewAuditResult("+ id +")'>审核失败,点击查看详情</label>";
    }
}

// 删除已选计划
function deleteButtonClick(id) {
    $.ajax({
        url:"../../plan/deletion/" + id,
        type:"GET",
        async: false,
        contentType:"application/x-www-form-urlencoded; charset=utf-8",
        dataType:"json",
        success:function(result){
            if(result.code==0){
                alert(result.message);
                location.href="./plan_list.html";
            }else if(result.code==1){
                alert(result.message);
            }
        }
    });
}

function previewAuditResult(id) {
    location.href="./audit_result_preview.html?id="+id;
}

function showAuditResult() {
    var url = window.location.href;
    var id = getArgsFromHref(url, "id");
    var tbody=window.document.getElementById("_audit_result");
    $.ajax({
        url:"../../plan/audit/reason/" + id,
        type:"GET",
        async: false,
        contentType:"application/x-www-form-urlencoded; charset=utf-8",
        dataType:"json",
        success:function(data){
            console.log(data);
            if(data.code==0){
                var str="";
                var tdata=data.object;
                for( i in tdata){
                    var str = "";
                    var j = parseInt(i)+1;
                    str += "<label> 素材" + j + "</label><br>" +
                        "<label> 链接：" + tdata[i].url + "</label><br>" +
                        "<label> 原因：" + tdata[i].remark + "</label><br>";
                    if (tdata[i].type == 0) {
                        str += "<img src='" + tdata[i].url + "' alt='image' width='30%' height='30%'>";
                    }
                    if (tdata[i].type == 1) {
                        str += "<video src='" + tdata[i].url + "'  width='640' height='480' controls='controls'> Your browser does not support the video tag. </video>";
                    }
                }
                tbody.innerHTML =str;
            }
        },
        error:function(){
            alert("search fails！");
        }
    });
}

function getArgsFromHref(sHref, sArgName)
{
    var args = sHref.split("?");
    var retval = "";
    if(args[0] == sHref) /*参数为空*/
    {
        return retval; /*无需做任何处理*/
    }
    var str = args[1];
    args = str.split("&");
    for(var i = 0; i < args.length; i ++)
    {
        str = args[i];
        var arg = str.split("=");
        if(arg.length <= 1) continue;
        if(arg[0] == sArgName) retval = arg[1];
    }
    return retval;
}

function locSearchSubmit(type) {
    var fdata;
    if (type == "origin") {
        fdata = null;
    } else {
        fdata = $("#loc_search_form").serialize();
    }
    var tbody = window.document.getElementById("loc_search_data");
    $.ajax({
        url:"../../locs/search",
        type:"POST",
        data:fdata,
        async: false,
        contentType:"application/x-www-form-urlencoded; charset=utf-8",
        dataType:"json",
        success:function(data){
            console.log(data);
            if(data.code==0){
                var str="";
                var tdata=data.object;
                for( i in tdata){
                    str +="<tr>" +
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\"><input type='checkbox' name='locs' value='" + tdata[i].id+ "'></span></div></td>"+
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">"+ tdata[i].id +"</span></div></td>"+
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">"+ tdata[i].name +"</span></div></td>"+
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">"+ tdata[i].province + "-"+ tdata[i].city + "-"+ tdata[i].district +"</span></div></td>"+
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">"+ tdata[i].height +"cm</span></div></td>"+
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">"+ tdata[i].width +"cm</span></div></td>"+
                        "</tr>";
                }
                tbody.innerHTML = str;
            }
        },
        error:function(){
            alert("search fails！");
        }
    });
}

function getProvinces() {
    var tbody=window.document.getElementById("_province");
    $.ajax({
        url:"../../loc/positions/0",
        type:"POST",
        dataType:"json",
        success:function(data){
            console.log(data);
            if(data.code==0){
                var str="<option value=''>选择省</option>";
                var tdata=data.object;
                for( i in tdata) {
                    str += "<option value='" + tdata[i] + "'>" + tdata[i] + "</option>";
                }
                tbody.innerHTML =str;
            }
        },
        error:function(){
            alert("search fails！");
        }
    });
}

function positionLinkage() {
    $("#_province").on("change",function(){
        getCities();
        getDistricts()
    });
    $("#_city").on("change",function(){
        getDistricts()
    });

}

function getCities() {
    var tbody=window.document.getElementById("_city");
    var optionProvince=$("#_province option:selected");  //获取选中的项
    $.ajax({
        url:"../../loc/positions/1",
        type:"POST",
        data:{province:optionProvince.val()},
        async: false,
        contentType:"application/x-www-form-urlencoded; charset=utf-8",
        dataType:"json",
        success:function(data){
            console.log(data);
            if(data.code==0){
                var str="<option value=''>选择市</option>";
                var tdata=data.object;
                for( i in tdata) {
                    str += "<option value='" + tdata[i] + "'>" + tdata[i] + "</option>";
                }
                tbody.innerHTML =str;
            }
        },
        error:function(){
            alert("search fails！");
        }
    });

}

function getDistricts() {
    var tbody=window.document.getElementById("_district");
    var optionProvince=$("#_province option:selected");
    var optionCity=$("#_city option:selected");
    $.ajax({
        url:"../../loc/positions/2",
        type:"POST",
        data:{province:optionProvince.val(), city:optionCity.val()},
        async: false,
        contentType:"application/x-www-form-urlencoded; charset=utf-8",
        dataType:"json",
        success:function(data){
            console.log(data);
            if(data.code==0){
                var str="<option value=''>选择区/县</option>";
                var tdata=data.object;
                for( i in tdata) {
                    str += "<option value='" + tdata[i] + "'>" + tdata[i] + "</option>";
                }
                tbody.innerHTML =str;
            }
        },
        error:function(){
            alert("search fails！");
        }
    });

}








